package presentation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpSession;


import metier.ThemeMetier;
import modele.Administrateur;
import modele.Annee;
import modele.Theme;


import org.apache.struts2.ServletActionContext;

import services.persistance.BaseDAO;

import com.opensymphony.xwork2.ActionSupport;

public class ThemeAction extends ActionSupport {

	private static final long serialVersionUID = 7608206044887440915L;
	public long identifiant;
	private String description;
	private Collection indicateurs;
	private ArrayList<Theme> listeThemes;

	public void ann() {
		long j;
		for (int i=0; i<3000; i++)
		{
			j=i;
			Annee an = new Annee(j,i);
			BaseDAO.getInstance().ajouter(an);
		}
	}

	
	
	public String listeThemes() {

		

		long idAdmin = (long) ServletActionContext.getRequest().getSession()
				.getAttribute("identifiant");
		listeThemes = (ArrayList<Theme>) ThemeMetier
				.getListeThemes(idAdmin);
		
		ServletActionContext.getRequest().getSession()
		.setAttribute("derniereListeTheme", listeThemes);

		return SUCCESS;
		
	}

	public String modifierTheme() {

		Theme theme = new Theme(description);
		theme.setIdentifiant(identifiant);
		theme.setIndicateurs(indicateurs);
		if (ThemeMetier.modifierTheme(theme)) {
			return getDerniereListeTheme();
		} else 
			return ERROR;
		
	}

	public String supprimerTheme() {
		ThemeMetier.supprimerTheme(identifiant);
		addActionMessage("Theme supprime");

		return getDerniereListeTheme();
	}

	public String ajouterTheme() {
		Theme theme = new Theme(description);
		long idAdmin = (long) ServletActionContext.getRequest().getSession()
				.getAttribute("identifiant");
		Administrateur admin = ThemeMetier.getAdministrateur(idAdmin);
		theme.setAdministrateur(admin);
		if (ThemeMetier.ajouterTheme(theme))
			return SUCCESS;
		else
			return ERROR;
		
	}

	public String rechercherTheme() {
		HttpSession session = ServletActionContext.getRequest().getSession();

		

		session.setAttribute("description", description);
		

		Theme theme = new Theme(description);
		Administrateur admin = new Administrateur();
		admin.setIdentifiant((long) session.getAttribute("identifiant"));
		theme.setAdministrateur(admin);

		listeThemes = ThemeMetier.rechercherThemes(theme);

		return SUCCESS;
	}

	//@SuppressWarnings("unchecked")
	public String getDerniereListeTheme() {
		HttpSession session = ServletActionContext.getRequest().getSession();
	//	listeThemes = (ArrayList<Theme>) session.getAttribute("derniereListeTheme");
		return SUCCESS;
	}

//	public String getDerniereListeTheme() {
//		HttpSession session = ServletActionContext.getRequest().getSession();
//		if ("listeRecherche".equals(session.getAttribute("derniereListeTheme")))
//			return "listeRecherche";
//		else
//			return "listeThemes";
//	}


	

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public ArrayList<Theme> getListeThemes() {
		return listeThemes;
	}

	public void setListeThemes(ArrayList<Theme> listeThemes) {
		this.listeThemes = listeThemes;
	}

	public long getIdentifiant() {
		return identifiant;
	}

	public void setIdentifiant(long identifiant) {
		this.identifiant = identifiant;
	}

}
